package com.sunshine.lnuplus.ui.activity

import android.content.*
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.base.BaseActivity
import com.sunshine.lnuplus.model.InitData
import com.sunshine.lnuplus.utils.LessonUtils
import com.sunshine.lnuplus.utils.StatusBarUtils
import com.sunshine.lnuplus.viewModel.SplashViewModel

class SplashActivity : BaseActivity() {

    private lateinit var viewModel: SplashViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        viewModel = ViewModelProvider(this).get(SplashViewModel::class.java)
        initView()
    }

    private fun initView() {
        StatusBarUtils.setStatusBarWithImage(this)
        StatusBarUtils.hideSystemUI(window)

        setNoTitle()

        if(LessonUtils.initLessonBean(
                viewModel.getAllLesson(),
                this,
                supportFragmentManager,
                viewModel.repository)) {
            viewModel.getLessonStyle().observe(this, Observer {lessonStyle ->
                InitData.lessonStyle = lessonStyle
                initData()
                viewModel.getLessonStyle().removeObservers(this)
            })
        }

    }

    private fun initData() {
        if(viewModel.isFirstStart()){
            viewModel.initSetting()
            viewModel.hasStarted()
            startActivity(Intent(this, WelcomeActivity::class.java))
            finish()
        }else{
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }
    }
}
